package com.yuebook.smartrpc.framework.invoker;

import com.yuebook.smartrpc.framework.model.SmartRpcRequest;
import com.yuebook.smartrpc.framework.model.SmartRpcResponse;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * @author honglei
 * @date 2018/11/22 下午4:52
 */
@Sharable
public class NettyClientInvokeInHandler extends SimpleChannelInboundHandler<SmartRpcResponse> {

    private SmartRpcRequest request;

    public NettyClientInvokeInHandler(SmartRpcRequest request) {
        this.request = request;
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        ctx.channel().writeAndFlush(request);
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, SmartRpcResponse o) throws Exception {
        InvokerResponseHolder.putResponse(o);
    }
}
